package com.huohuzhihui.api.service.impl;

import com.huohuzhihui.api.exception.VideoException;
import com.huohuzhihui.api.service.ApiDyActivityVideoService;
import com.huohuzhihui.api.service.ApiNoticeService;
import com.huohuzhihui.common.config.RuoYiConfig;
import com.huohuzhihui.common.constant.Constants;
import com.huohuzhihui.common.utils.DateUtils;
import com.huohuzhihui.common.utils.StringUtils;
import com.huohuzhihui.common.utils.mpeg.FfmpegUtils;
import com.huohuzhihui.douyin.basis.body.video.AutoCreateVideoParam;
import com.huohuzhihui.douyin.basis.response.video.VideoCreateRes;
import com.huohuzhihui.douyin.basis.service.interfaces.DyBasisService;
import com.huohuzhihui.douyin.common.enums.ApiPlatform;
import com.huohuzhihui.douyin.domain.DouyinUserVideo;
import com.huohuzhihui.douyin.service.IDouyinUserService;
import com.huohuzhihui.douyin.service.IDouyinUserVideoService;
import com.huohuzhihui.merchant.domain.MerActivityVideo;
import com.huohuzhihui.merchant.domain.MerCouponActivity;
import com.huohuzhihui.merchant.domain.MerResource;
import com.huohuzhihui.merchant.service.IMerActivityVideoService;
import com.huohuzhihui.merchant.service.IMerResourceService;
import com.huohuzhihui.system.domain.SysNotice;
import com.huohuzhihui.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单Service业务层处理
 * 
 * @author huohuzhihui
 * @date 2020-11-15
 */
@Service
@EnableAsync
public class ApiDyActivityVideoServiceImpl implements ApiDyActivityVideoService {
    @Autowired
    private IMerResourceService merResourceService;
    @Autowired
    private IMerActivityVideoService merActivityVideoService;
    @Resource
    private DyBasisService dyBasisService;
    @Resource
    private IDouyinUserVideoService douyinUserVideoService;
    @Resource
    private IDouyinUserService douyinUserService;

    @Override
    public String mergeActivityVideo(MerCouponActivity merCouponActivity) {
        // 本地资源路径
        String localPath = RuoYiConfig.getProfile();
        //合成视频
        //按视频分组查找视频，每个分组随机取一个进行合成，把取到的视频记录下来，下次取的时候过滤掉
        String resourceGroupIds = merCouponActivity.getResourceGroupId();
        String[] groupIdAry = StringUtils.split(resourceGroupIds,",");

        List<String> videoPaths = new ArrayList<String>();
        if(groupIdAry!=null && groupIdAry.length>0) {
            for (int i = 0; i < groupIdAry.length; i++) {
                long resoureGroupId = Long.parseLong(groupIdAry[i]);
                MerResource merResource = this.merResourceService.getRandMerReource(resoureGroupId, merCouponActivity.getId());
                if(merResource==null){
                    continue;
                }
                videoPaths.add((localPath + StringUtils.substringAfter(merResource.getResourceUrl(), Constants.RESOURCE_PREFIX)).replaceAll("/", "\\\\"));
                //添加转场视频
              /*  String zhuanchangFilePath = "G:\\迅雷下载\\微信小商店\\36e8f572bc50ab7add0a3295b271b6e8\\20个炫光转场视频素材\\20组\\1.mp4";
                videoPaths.add(zhuanchangFilePath);*/

                MerActivityVideo merActivityVideo = new MerActivityVideo();
                merActivityVideo.setActivityId(merCouponActivity.getId());
                merActivityVideo.setResourceId(merResource.getId());

                this.merActivityVideoService.insertMerActivityVideo(merActivityVideo);
            }
            //合成视频
            String mergePath = FfmpegUtils.mergeVideos(videoPaths);
            return mergePath;
        }
        return null;
    }

    @Async
    @Override
    public void pushVideo(MerCouponActivity merCouponActivity, String openId) {
        String videoPath = mergeActivityVideo(merCouponActivity);

        AutoCreateVideoParam autoCreateVideoParam = new AutoCreateVideoParam();

        String text = merCouponActivity.getVideoTitle();
        if(StringUtils.isNotEmpty(merCouponActivity.getDouyinTitle())){//话题
            text = text + merCouponActivity.getDouyinTitle();
        }
        if(StringUtils.isNotEmpty(merCouponActivity.getAtNicknames())){//@他人
            String[] atNickNames = merCouponActivity.getAtNicknames().split(",");
            String[]  atOpenIds = merCouponActivity.getAtUsers().split(",");

            autoCreateVideoParam.setAtUsers(atOpenIds);
            for(int i = 0 ; i < atNickNames.length; i++){
                text = text + "@"+ atNickNames[i];
            }
        }
        autoCreateVideoParam.setText(text);
        try {
            VideoCreateRes.VideoCreateResData videoCreateResData = dyBasisService.getVideoService().autoCreateVideo(openId,new File(videoPath),autoCreateVideoParam, ApiPlatform.DOU_YIN);

            //保存用户视频发送记录
            DouyinUserVideo douyinUserVideo = new DouyinUserVideo() ;
            douyinUserVideo.setDouyinUserId(this.douyinUserService.selectDouyinUserByOpenId(openId).getId());
            douyinUserVideo.setCreateTime(DateUtils.getNowDate());
            douyinUserVideo.setItemId(videoCreateResData.getItemId());
            douyinUserVideo.setMerchantId(merCouponActivity.getMerchantId());
            douyinUserVideo.setActivityId(merCouponActivity.getId());
            douyinUserVideo.setStatus(3L);//审核中
            this.douyinUserVideoService.insertDouyinUserVideo(douyinUserVideo);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }





}

